package com.beehoo.dcportal.admin.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.beehoo.dcportal.admin.dao.OrganizationDao;
import com.beehoo.dcportal.common.base.BaseDao;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.OrganizationBean;

public class OrganizationDaoImpl extends BaseDao implements OrganizationDao {

	private static final Logger logger = Logger.getLogger(OrganizationDaoImpl.class);
	
	@Override
	public String addOrg(OrganizationBean organizationBean)
			throws BaseException {
		String id = null;
		try {
			id = (String) sqlMapClient.insert("admin-org.addOrg", organizationBean);
			logger.info("中心管理-添加组织机构|#[中心编号：" 
					+ organizationBean.getStreetId()
					+ "],[机构名称：" + organizationBean.getOrgName()
					+ "],[机构头像：" + organizationBean.getOrgPhoto()
					+ "],[机构负责人：" + organizationBean.getOrgOwner()
					+ "],[机构职责：" + organizationBean.getOrgDuty()
					+ "],[机构电话：" + organizationBean.getOrgTel()
					+ "],[机构Email：" + organizationBean.getOrgEmail()
					+ "],成功!：");
		} catch (SQLException e) {
			logger.error("中心管理-添加组织机构|#[中心编号：" 
					+ organizationBean.getStreetId()
					+ "],[机构名称：" + organizationBean.getOrgName()
					+ "],[机构头像：" + organizationBean.getOrgPhoto()
					+ "],[机构负责人：" + organizationBean.getOrgOwner()
					+ "],[机构职责：" + organizationBean.getOrgDuty()
					+ "],[机构电话：" + organizationBean.getOrgTel()
					+ "],[机构Email：" + organizationBean.getOrgEmail()
					+ "],失败!异常："+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.org.create.error");
		}
		return id;
	}

	@Override
	public int delOrg(String orgId) throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.delete("admin-org.delOrg", orgId);
			logger.info("中心管理-删除组织机构|#[机构编号：" + orgId
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("中心管理-删除组织机构|#[机构编号：" + orgId
					+ "],失败!异常："+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.org.del.error");
		}
		return ret;
	}

	@Override
	public OrganizationBean queryOrg(Map<String, Object> paramMap)
			throws BaseException {
		OrganizationBean bean = null;
		try {
			bean = (OrganizationBean) sqlMapClient.queryForObject("admin-org.queryOrg", paramMap);
			logger.info("中心管理-查询组织机构|#[中心编号："
					+ paramMap.get("streetId")
					+ "],[机构编号：" + paramMap.get("orgId")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("中心管理-查询组织机构|#[中心编号："
					+ paramMap.get("streetId")
					+ "],[机构编号：" + paramMap.get("orgId")
					+ "],失败!异常："+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.org.query.error");
		}
		return bean;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrganizationBean> queryOrgs(Map<String, Object> paramMap)
			throws BaseException {
		List<OrganizationBean> beanLs = null;
		try {
			beanLs = sqlMapClient.queryForList("admin-org.queryOrg", paramMap);
			logger.info("中心管理-查询组织机构|#[中心编号："
					+ paramMap.get("streetId")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("中心管理-查询组织机构|#[中心编号："
					+ paramMap.get("streetId")
					+ "],失败!异常："+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.org.query.error");
		}
		return beanLs;
	}

	@Override
	public int updateOrg(OrganizationBean organizationBean)
			throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.update("admin-org.updateOrg", organizationBean);
			logger.info("中心管理-更新组织机构|#[中心编号：" 
					+ organizationBean.getStreetId()
					+ "],[机构编号：" + organizationBean.getOrgId()
					+ "],[机构名称：" + organizationBean.getOrgName()
					+ "],[机构头像：" + organizationBean.getOrgPhoto()
					+ "],[机构负责人：" + organizationBean.getOrgOwner()
					+ "],[机构职责：" + organizationBean.getOrgDuty()
					+ "],[机构电话：" + organizationBean.getOrgTel()
					+ "],[机构Email：" + organizationBean.getOrgEmail()
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("中心管理-更新组织机构|#[中心编号：" 
					+ organizationBean.getStreetId()
					+ "],[机构编号：" + organizationBean.getOrgId()
					+ "],[机构名称：" + organizationBean.getOrgName()
					+ "],[机构头像：" + organizationBean.getOrgPhoto()
					+ "],[机构负责人：" + organizationBean.getOrgOwner()
					+ "],[机构职责：" + organizationBean.getOrgDuty()
					+ "],[机构电话：" + organizationBean.getOrgTel()
					+ "],[机构Email：" + organizationBean.getOrgEmail()
					+ "],失败!异常："+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.org.update.error");
		}
		return ret;
	}

}
